package de.swta.freezay.admin.reservation;
import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Item;
import de.swta.freezay.database.dbEntities.ItemPackage;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.NumberFormatter;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 *
 * @author Jan
 */
public abstract class PackageContentView extends Panel {

    /** Formatter for numvber format. */
    private static final NumberFormatter NUMBER_FORMATTER =
                    new NumberFormatter(new DecimalFormat("####.##"));

    /**
     * Creates a new instance of PackageContentView.
     *
     * @param id of the ContentView.
     * @param packageId Id of the package.
     */
    public PackageContentView(String id, Integer packageId) {
        super (id);
        init(packageId);
    }

    /**
     * Initializes the components of the panel.
     *
     * @param packageId Id of the package.
     */
    private void init(Integer packageId) {

        /* List of the items in package. */
        List<ItemPackage> items =
                JPAController.PackageJpaController.findPackage(packageId).
                                                    getItemPackageCollection();

        final ListView<ItemPackage> itemView =
                            new ListView<ItemPackage>("allItems", items) {

            @Override
            protected void populateItem(ListItem<ItemPackage> item) {
                Item i = item.getModelObject().getItem();

                /* Display one item in a row. */
                item.add(new Label("Name", "" + i.getName()));
                item.add(new Label("Category",
                        i.getCategory().getName()));
                try {
                    item.add(new Label("Amount",
                            NUMBER_FORMATTER.valueToString(i.getAmount())
                                + " " + i.getUnit().getName()));

                } catch (ParseException ex) {
                    Logger.getLogger(PackageContentView.class.getName()).log(
                            Level.SEVERE, null, ex);
                }
                item.add(new Label("Count", "" + item.getModelObject().getItemCount()));
            }
        };

        add(itemView);

        /* CloseLink. */
        add(new AjaxLink("closeLink") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                PackageContentView.this.onCloseClicked(target);
            }
        });
    }

    /**
     * Handles the actions after the close link was clicked.
     *
     * @param target AjaxRequestTarget.
     */
    public abstract void onCloseClicked(AjaxRequestTarget target);

}
